package org.rcgwt.client.ui.grid.model;

/**
 * 
 * @author Olivier Oeuillot (latest modification by $Author: $)
 * @version $Revision: $ $Date: $
 */
public class BasicColumn implements IColumn {

	private int titleColSpan = 1;

	private final String name;

	private final String title;

	private boolean isSortable;

	private boolean isVisible;

	private int width;

	private int minWidth = -1;

	private int maxWidth = -1;

	private int percentWidth;

	private boolean resizable;

	public BasicColumn(String name, String title) {
		this.name = name;
		this.title = title;
	}

	public int getPercentWidth() {
		return percentWidth;
	}

	public IColumn setPercentWidth(int percentWidth) {
		this.percentWidth = percentWidth;

		return this;
	}

	@Override
	public int getTitleColSpan(int titleRowIndex) {
		return titleColSpan;
	}

	@Override
	public String getName() {
		return name;
	}

	@Override
	public String getMainTitle() {
		return title;
	}

	@Override
	public String getTitle(int rowIndex) {
		return getMainTitle();
	}

	@Override
	public boolean isSortable() {
		return isSortable;
	}

	@Override
	public boolean isVisible() {
		return isVisible;
	}

	@Override
	public boolean isResizable() {
		return resizable;
	}

	@Override
	public int getWidth() {
		return this.width;
	}

	public void setSortable(boolean isSortable) {
		this.isSortable = isSortable;
	}

	public void setVisible(boolean isVisible) {
		this.isVisible = isVisible;
	}

	public IColumn setWidth(int width) {
		this.width = width;

		return this;
	}

	public IColumn setResizable(boolean resizable) {
		this.resizable = resizable;

		return this;
	}

	@Override
	public int getMaxWidth() {
		return maxWidth;
	}

	@Override
	public int getMinWidth() {
		return minWidth;
	}

	@Override
	public boolean equals(Object obj) {
		if (obj instanceof IColumn) {
			return ((IColumn) obj).getName().equals(name);
		}
		return false;
	}

	@Override
	public IColumn setMaxWidth(int width) {
		maxWidth = width;

		return this;
	}

	@Override
	public IColumn setMinWidth(int width) {
		minWidth = width;

		return this;
	}

	@Override
	public boolean isValidWidth(int width) {
		return ((((minWidth > -1) && (width >= minWidth)) || (minWidth < 0)) && (((maxWidth > -1) && (width <= maxWidth)) || (maxWidth < 0)));
	}
}
